<template>
  <section class="pb-fix">
    <div class="container-box">
      <header class="header">
        <div class="flex-l">
          <span class="title">{{ t('quotes') }}</span>
        </div>
        <div class="icon-group">
          <div class="icon search" @click="onRoute('/optional/search')">
            <img src="@/assets/image/optional/search.png" alt="">
          </div>
          <!-- <div class="icon setting">
            <img src="@/assets/image/quotes/option.png" alt="" @click="onRoute('/optional/setting')">
          </div> -->
        </div>
      </header>
      <section class="tab-container">
        <van-tabs v-model:active="tabActive" shrink @click-tab="onClickTab">
          <van-tab title="ETF">
            <section v-if="tabActive === 0" class="etf-container">
              <div class="kline-chart-container">
                <div class="kline-chart" id="lineChart"></div>
                <p class="max green">+0.39%</p>
                <p class="min red">-1.62%</p>
                <div class="time-x">
                  <span>09:30</span>
                  <span>16:00</span>
                </div>
              </div>
              <div class="indicator-container flex">
                <!-- <div :class="{ 'indicator-item': true, 'active-item': index === typeIndex }" -->
                <div :class="{ 'indicator-item': true }" v-for="(item, index) in indexCardInfo" :key="item"
                  @click="typeIndex = index">
                  <div class="title-box">
                    <div class="point"></div>
                    <p class="title">{{ t(item.name) }}</p>
                  </div>
                  <p class="num">{{ item.close ?? '--' }}</p>
                  <p class="value green">{{ item.changeRatio ? `${item.changeRatio}%` : item.changeRatio === 0 ? 0
                    :
                    '--' }}</p>
                </div>
              </div>
              <div class="nav">
                <etf-ex-nav :defaultEtfListData="defaultEtfListData" />
              </div>
              <div class="banner-container">
                <van-swipe class="swipe-box" :autoplay="5000" indicator-color="white">
                  <van-swipe-item v-for="(item) in newsList" :key="item"
                    @click="onRoute(`/cryptos/announceDetail?id=${item.uuid}`)">
                    <header class="flex header">
                      <span class="title"> {{ item?.title }}</span>
                      <span class="date"> {{ item?.createTime }}</span>
                    </header>
                    <div class="content van-multi-ellipsis--l2" v-html="formatNews(item.content)"></div>
                  </van-swipe-item>
                </van-swipe>
              </div>
              <div class="divider"></div>
              <div class="hot-container">
                <div class="header-box">
                  <p class="title">{{ t('hotPlate') }}</p>
                  <div class="icon-group" @click="onRoute('/quotes/hotModules')">
                    <p class="text">{{ t('更多') }}</p>
                    <img src="@/assets/image/quotes/right-arrow.png" alt="" class="icon arrow">
                  </div>
                </div>
                <ul class="hot-box">
                  <li class="hot-item" v-for="value in hotSymbol" :key="value" @click="itemClick(value)">
                    <p class="name">
                      {{ value.name }}
                    </p>
                    <p class="value"> {{ value.close ?? '--' }}</p>
                    <p class="num">
                      <span class="num-left" :class="value.chg > 0 ? 'text-up' : 'text-down'">{{ formatNumber(value.chg)
                      }}</span>
                      <span class="num-right" :class="value.changeRatio > 0 ? 'text-up' : 'text-down'">{{
                        value.changeRatio ? `${value.changeRatio}%` : value.changeRatio === 0 ? 0
                        :
                        '--' }}</span>
                    </p>
                  </li>
                </ul>
              </div>
              <div class="divider"></div>
              <div class="all-etf-ranking">
                <p class="title">{{ t('AllETFRankings') }}</p>
                <div class="tabs flex">
                  <div class="tab-item flex-1" v-for="(item, index) in allEtfTabList"
                    @click="selectAllEtfIndex(index, item.value)" :class="[allEtfTabIndex === index ? 'active' : '']"
                    :key="item">
                    {{ item.title }}
                  </div>
                </div>
                <div class="etf-table">
                  <ul>
                    <li class="title-line">
                      <div class="flex-l">
                        <p>{{ t('nameCode') }}</p>
                      </div>
                      <div class="flex-r">
                        <div class="flex-r-item">
                          <p>{{ t('uptodate') }}</p>
                        </div>
                        <div class="flex-r-item">
                          <p>{{ t('increase') }}</p>
                        </div>
                        <div class="flex-r-item">
                          <p>{{ t('SparkDiagram') }}</p>
                        </div>
                        <div class="flex-r-item right">
                          <p>{{ t('Amount') }}</p>
                        </div>
                      </div>
                    </li>
                    <li v-for="(item, index) in allEtfListData" :key="item.symbol" @click="itemClick(item)">
                      <div class="flex-l">
                        <p>{{ item.name }}</p>
                        <p class="gray-text">
                          {{ item.symbol }}
                        </p>
                      </div>
                      <div class="flex-r">
                        <div class="flex-r-item">
                          <p :class="item.close < 1 ? 'text-up' : 'text-down'">{{
                            item.close
                          }}</p>
                        </div>
                        <div class="flex-r-item">
                          <p :class="item.changeRatio > 0 ? 'text-up' : 'text-up'">
                            <span>{{ item.changeRatio }}%</span>
                          </p>
                        </div>
                        <div class="flex-r-item">
                          <div class="mecharts-box">
                            <m-echarts :dataObj="item" :ratio="item.changeRatio" :index="index" />
                          </div>
                        </div>
                        <div class="flex-r-item">
                          <div class="last-item right">
                            <p>
                              <span>{{ item.amount }}</span>
                            </p>
                          </div>
                        </div>
                      </div>
                    </li>
                  </ul>
                </div>
                <div class="more-box">
                  <div class="icon-group" @click="onRoute('/quotes/hotModules')">
                    <p class="text">{{ t('更多') }}</p>
                    <img src="@/assets/image/quotes/right-arrow.png" alt="" class="icon arrow">
                  </div>
                </div>
              </div>
              <div class="divider"></div>
              <div class="other-etf-ranking">
                <p class="title">{{ t('OtherETFRankings') }}</p>
                <div class="tabs flex">
                  <div class="tab-item flex-1" v-for="(item, index) in otherEtfTabList"
                    @click="selectOtherEtfIndex(index, item.value)" :class="[otherEtfTabIndex === index ? 'active' : '']"
                    :key="item">
                    {{ item.title }}
                  </div>
                </div>
                <div class="etf-table">
                  <ul>
                    <li class="title-line">
                      <div class="flex-l">
                        <p>{{ t('nameCode') }}</p>
                      </div>
                      <div class="flex-r">
                        <div class="flex-r-item">
                          <p>{{ t('uptodate') }}</p>
                        </div>
                        <div class="flex-r-item">
                          <p>{{ t('increase') }}</p>
                        </div>
                        <div class="flex-r-item">
                          <p>{{ t('SparkDiagram') }}</p>
                        </div>
                        <div class="flex-r-item right">
                          <p>{{ t('Amount') }}</p>
                        </div>
                      </div>
                    </li>
                    <li v-for="(item, index) in otherEtfListData" :key="item.symbol" @click="itemClick(item)">
                      <div class="flex-l">
                        <p>{{ item.name }}</p>
                        <p class="gray-text">
                          {{ item.symbol }}
                        </p>
                      </div>
                      <div class="flex-r">
                        <div class="flex-r-item">
                          <p :class="item.close < 1 ? 'text-up' : 'text-down'">{{
                            item.close
                          }}</p>
                        </div>
                        <div class="flex-r-item">
                          <p :class="item.changeRatio > 0 ? 'text-up' : 'text-down'">
                            <span>{{ item.changeRatio }}%</span>
                          </p>
                        </div>
                        <div class="flex-r-item">
                          <m-echarts :dataObj="item" :ratio="item.changeRatio" :index="index + 999" />
                        </div>
                        <div class="flex-r-item">
                          <div class="last-item right">
                            <p>
                              <span>{{ item.amount }}</span>
                            </p>
                          </div>
                        </div>
                      </div>
                    </li>
                  </ul>
                </div>
                <div class="more-box">
                  <div class="icon-group" @click="onRoute('/quotes/hotModules')">
                    <p class="text">{{ t('更多') }}</p>
                    <img src="@/assets/image/quotes/right-arrow.png" alt="" class="icon arrow">
                  </div>
                </div>
              </div>
              <div class="divider"></div>
            </section>
          </van-tab>
          <van-tab :title="t('加密货币')">
            <Cryptos id="cryptos" />
          </van-tab>
          <van-tab :title="t('外汇')">
            <Foreign v-if="tabActive === 2" />
          </van-tab>
          <van-tab :title="t('UsStocks')">
            <UsStock />
          </van-tab>
          <!-- <van-tab :title="t('A股')">
            <div class="soon-container">
              <div>
                <img src="../../assets/image/coming.png" alt="">
                <p>{{ t('模块建设中') }}...</p>
              </div>
            </div>
          </van-tab>
          <van-tab :title="t('港股')">
            <div class="soon-container">
              <div>
                <img src="../../assets/image/coming.png" alt="">
                <p>{{ t('模块建设中') }}...</p>
              </div>
            </div>
          </van-tab>
          <van-tab :title="t('台股')">
            <div class="soon-container">
              <div>
                <img src="../../assets/image/coming.png" alt="">
                <p>{{ t('模块建设中') }}...</p>
              </div>
            </div>
          </van-tab> -->
        </van-tabs>
      </section>
    </div>
  </section>
</template>
    
<script setup>
import { ref, onMounted, onBeforeUnmount, onUnmounted, nextTick } from 'vue';
import { useUserStore } from '@/store/user';
import { useRoute, useRouter } from 'vue-router';
import EtfExNav from "@/components/trade/etf-ex-nav/index.vue"
import {
  _getETFItemList
} from "@/service/etf.api.js";
import { _getQuotes } from '@/service/quotes.api'
import { useI18n } from 'vue-i18n'
import * as echarts from 'echarts/core';
import {
  DatasetComponent,
  TitleComponent,
  TooltipComponent,
  GridComponent,
  TransformComponent
} from 'echarts/components';
import { LineChart } from 'echarts/charts';
import { LabelLayout, UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
import linesData from './data.json'
import Foreign from '../foreign/index.vue'
import Cryptos from '../cryptos/index.vue'
import UsStock from '../usStock/List.vue'
import MEcharts from "@/components/ex-echarts/index.vue";
import { _getNewsList1 } from '@/service/user.api'
import { formatNews } from '@/utils'

echarts.use([
  DatasetComponent,
  TitleComponent,
  TooltipComponent,
  GridComponent,
  TransformComponent,
  LineChart,
  CanvasRenderer,
  LabelLayout,
  UniversalTransition
]);

const { t } = useI18n()
const hotSymbol = ref([1, 2, 3, 4, 5, 6])
const allEtfTabIndex = ref(0)
const otherEtfTabIndex = ref(0)
const allEtfListData = ref([])
const otherEtfListData = ref([])
const defaultEtfListData = ref([])
const indexCardInfo = ref([])
const newsList = ref([])
const interval = ref(null)
const sortType = ref('changeRatio') // 全部ETF排名 默认涨幅榜
const otherType = ref('global') // 其他ETF排名 全球ETF
const typeIndex = ref(0)
const router = useRouter()
const route = useRoute()
const defaultTabActive = +route.query.tabActive || 0
const tabActive = ref(defaultTabActive)
const chartDom = ref(null)
let myChart = null

const allEtfTabList = ref([
  { title: t('涨幅榜'), value: "changeRatio" },
  { title: t('跌幅榜'), value: "decrease" },
  { title: t('成交额'), value: "amount" },
])
const otherEtfTabList = ref([
  { title: t('GlobalETFs'), value: "global" },
  { title: t('EnergyETFs'), value: "energy" },
  { title: t('GoldEFs'), value: "gold" },
  { title: t('ArtificialIntelligenceETFs'), value: "ai" },
])

onMounted(async () => {
  getNewsList()
  getETFItemList()
  getIndexCardInfo()
  interval.value = setInterval(() => {
    getETFItemList()
    getIndexCardInfo()
  }, 3000)
  nextTick(() => {
    renderChart(linesData)
  })
})

onBeforeUnmount(() => {
  if (interval.value) {
    clearInterval(interval.value)
  }
})

onUnmounted(() => {
  myChart && myChart.dispose()
})

const renderChart = (_rawData) => {
  chartDom.value = document.getElementById('lineChart')
  if (!chartDom.value) return
  myChart = echarts.init(chartDom.value);
  let option;

  const countries = [
    'France',
    'Russia',
    'United Kingdom'
  ];
  const datasetWithFilters = [];
  const seriesList = [];
  echarts.util.each(countries, function (country) {
    const datasetId = 'dataset_' + country;
    datasetWithFilters.push({
      id: datasetId,
      fromDatasetId: 'dataset_raw',
      transform: {
        type: 'filter',
        config: {
          and: [
            { dimension: 'Year', gte: 1950 },
            { dimension: 'Country', '=': country }
          ]
        }
      }
    });
    seriesList.push({
      type: 'line',
      datasetId: datasetId,
      showSymbol: false,
      name: country,
      endLabel: {
        show: false,
      },
      labelLayout: {
        moveOverlap: 'shiftY'
      },
      emphasis: {
        focus: 'series'
      },
      encode: {
        x: 'Year',
        y: 'Income',
        label: ['Country', 'Income'],
        itemName: 'Year',
        tooltip: ['Income']
      }
    });
  });
  option = {
    color: ['#3A6DC4', '#5F42E7', '#E3702C'],
    animationDuration: 0,
    dataset: [
      {
        id: 'dataset_raw',
        source: _rawData
      },
      ...datasetWithFilters
    ],
    tooltip: {
      order: 'valueDesc',
      trigger: 'axis',
      show: false,
    },
    xAxis: {
      type: 'category',
      nameLocation: 'middle',
      show: false,
    },
    yAxis: {
      name: 'Income',
      show: false,
    },
    grid: {
      top: 20,
      bottom: 0,
      left: 0,
      right: 0,
    },
    series: seriesList
  };
  myChart.setOption(option);

  option && myChart.setOption(option);
}

const getIndexCardInfo = () => {
  _getETFItemList({
    type: 'indices',
    symbol: ['.DJI', '.INX', '.IXIC']
  }).then(data => {
    indexCardInfo.value = data || []
  })
}

const getNewsList = () => {
  _getNewsList1({
    language: JSON.parse(localStorage.getItem('lang')) || 'en',
  }).then(res => {
    const result = res.filter(item => item.show)
    newsList.value = result.slice(0, 3)
  })
}

const getETFItemList = () => {
  _getETFItemList({
    type: 'indices'
  }).then(data => {
    if (data === null) {
      data = []
    }
    defaultEtfListData.value = data
    hotSymbol.value = data.slice(0, 6)
    const copyData = Array.from(data)
    const type = sortType.value;
    let result = []
    if (type === 'decrease') {
      result = copyData.sort((a, b) => a.changeRatio - b.changeRatio)
    } else {
      result = copyData.sort((a, b) => b[type] - a[type])
    }
    allEtfListData.value = result
    otherEtfListData.value = data.filter(item => item.category === otherType.value) || []
  })
}

const selectAllEtfIndex = (index, value) => {
  allEtfTabIndex.value = index
  sortType.value = value
  let result = []
  if (sortType.value === 'decrease') {
    result = defaultEtfListData.value.sort((a, b) => a.changeRatio - b.changeRatio)
  } else {
    result = defaultEtfListData.value.sort((a, b) => b[sortType.value] - a[sortType.value])
  }
  allEtfListData.value = result
}

const selectOtherEtfIndex = (index, value) => {
  otherEtfTabIndex.value = index
  otherType.value = value
  otherEtfListData.value = defaultEtfListData.value.filter(item => item.category === otherType.value)
}

const onClickTab = ({ name, tile }) => {
  tabActive.value = name
  if (name === 0) {
    nextTick(() => {
      renderChart(linesData)
    })
  } else if (name === 3) {
    // nextTick(() => {
    //   renderChart(linesData)
    // })
  }
};

const formatNumber = (num) => {
  if (num === null || num === undefined) return '--'
  if (num === 0) return 0
  if (num > 0) {
    return `+${num}`
  }
  if (num < 0) {
    return num
  }
}

const onRoute = (path) => {
  router.push(path)
}

const itemClick = (item) => {
  router.push(`/quotes/detail?symbol=${item.symbol}`)
}

const handleClickHotSymbol = (value) => {
  // console.log(value, 'value')
}

</script>
<style lang="scss" scoped>
:deep(.van-tabs__nav) {
  background: $selectSymbol_background;
}

:deep(.van-tab) {
  font-size: 14px;
  color: $text_color;
  font-weight: 400;
}

:deep(.van-tab.van-tab--active) {
  font-weight: 700;
}

:deep(.van-tabs__line) {
  background: url('../../assets/image/active.png') no-repeat center;
  width: 9px;
  height: 8px;
  background-size: 100% 100%;
}

.soon-container {
  padding: 0 16px;
  color: #ccc;
  font-size: 14px;
  display: flex;
  justify-content: center;
  text-align: center;

  img {
    width: 200px;
    height: 220px;
  }
}

.container-box {
  .green {
    color: $green;
  }

  .red {
    color: $red;
  }

  .header {
    display: flex;
    height: 28px;
    padding: 0 12px;

    .flex-l {
      flex: 1;
      display: inline-flex;

      .icon {
        display: inline-block;
        width: 24px;
        height: 28px;
        padding: 6px 4px;

        img {
          height: 16px;
          width: 12px;
        }
      }
    }

    .title {
      font-weight: 700;
      font-size: 20px;
      line-height: 28px;
      color: $mainTextColor;
    }

    .icon-group {
      width: 100px;
      text-align: right;

      .icon {
        display: inline-block;
        width: 28px;
        height: 28px;
        padding: 4px;
        margin-left: 16px;
      }
    }


  }

  .tab-container {
    margin-top: 18px;
  }

  .etf-container {
    padding: 0;

    .kline-chart-container {
      position: relative;

      .kline-chart {
        width: 100%;
        height: 200px;
        border: 1px solid $border_color;
      }

      .max {
        position: absolute;
        top: 5px;
        right: 10px;
        z-index: 2;
        font-size: 12px;
      }

      .min {
        position: absolute;
        z-index: 2;
        bottom: 35px;
        right: 10px;
        font-size: 12px;
      }
    }

    .time-x {
      padding: 0 12px 8px;
      font-size: 10px;
      color: #747A8F;
      display: flex;
      justify-content: space-between;
    }

    .indicator-container {
      padding: 0 12px;

      .active-item {
        border: 1px solid $active_line !important;
        border-radius: 10px;
      }

      .indicator-item {
        border: 1px solid transparent;
        flex: 1;
        padding-left: 6px;
        text-align: left;
        margin: 0 4px;
        border-radius: 10px;
        border: 1px solid $active_line !important;
        background: #1A2136;

        .title-box {
          display: inline-flex;
          align-items: center;
        }

        .title {
          position: relative;
          font-size: 12px;
          line-height: 16px;
          color: #B8BDD1;
        }

        .point {
          margin-right: 4px;
          width: 6px;
          height: 6px;
          border-radius: 50%;
        }

        .num {
          margin-left: 10px;
          font-weight: 700;
          font-size: 16px;
          line-height: 24px;
          color: $text_color;
        }

        .value {
          margin-left: 10px;
          font-size: 12px;
          font-weight: 400;
        }
      }

      .indicator-item:first-child .point {
        background: #3A6DC4;
      }

      .indicator-item:nth-child(2) .point {
        background: #5F42E7;
      }

      .indicator-item:last-child .point {
        background: #E3702C;
      }
    }

    .banner-container {
      margin: 20px 0;
      padding: 0 12px;

      .swipe-box {
        border-radius: 10px;
      }

      .van-swipe-item {
        height: 110px;
        padding: 10px 14px;
        color: $text_color;
        font-size: 14px;
        background: linear-gradient(180deg, #332D2E 0%, #24242E 100%);

        .header {
          padding: 0;
          display: flex;
          justify-content: space-between;
          align-items: center;

          .title {
            font-size: 14px;
            text-overflow: ellipsis;
            overflow: hidden;
            word-break: break-all;
            white-space: nowrap;
            max-width: 160px;
          }

          .title:first-letter {
            color: #E69A38;
          }

          .date {
            font-size: 12px;
            color: #B8BDD1;
            width: 130px;
            text-align: right;
          }
        }
      }
    }

    .hot-container {
      margin: 20px 0;
      padding: 0 8px;

      .header-box {
        display: flex;

        .title {
          flex: 1;
          font-size: 16px;
          padding: 0 8px;
          font-weight: 700;
        }

        .icon-group {
          display: flex;
          align-items: center;
          font-size: 14px;

          .icon.arrow {
            margin-left: 10px;
            width: 7px;
            height: 9px;
          }
        }

      }

      .hot-box {
        display: grid;
        grid-template-columns: 33.33% 33.33% 33.33%;
        grid-template-rows: repeat(2, 100px);
      }

      .hot-item {
        padding: 6px 2px;
        margin: 4px;
        text-align: center;
        font-size: 12px;
        line-height: 18px;
        color: #B8BDD1;
        background: #1B2134;
        border-radius: 4px;

        .value {
          font-weight: 700;
          color: $text_color;
          line-height: 24px;
        }

        .num {
          .num-left {
            margin-right: 6px;
          }
        }
      }
    }

    .all-etf-ranking,
    .other-etf-ranking {
      margin-top: 10px;

      .title {
        font-weight: 700;
        padding: 0 12px;
      }

      .tabs {
        padding: 0 12px;
        margin-top: 10px;
        // height: 40px;
        min-height: 40px;
        line-height: 24px;
        color: #BBBCBD;

        .tab-item {
          margin: 4px;
          text-align: center;
          padding: 4px 4px;
          font-size: 12px;
          color: $text_color5;
          background: $US_tab_background;
          border-radius: 10px;
          background-size: cover;
        }

        .active {
          font-weight: 400;
          color: $color_main !important;
          background: $US_tabActice_background;
          border-radius: 10px;
          background-size: cover;
        }
      }

      .etf-table {

        .right {
          text-align: right;
        }

        ul {
          margin-top: 10px;
        }

        .title-line {
          color: #747A8F;
          font-size: 12px;
          font-weight: 400;
          padding: 0 12px;
          border: none;
        }

        li {
          padding: 14px 12px;
          display: flex;
          align-items: center;
          font-size: 12px;
          line-height: 18px;
          border-bottom: 1px solid $border_color;

          .gray-text {
            color: #BCBDC2;
            font-size: 12px;
          }

          .flex-l {
            width: 100px;
          }

          .flex-r {
            display: inline-flex;
            flex: 1;

            .flex-r-item {
              flex: 1;
              align-self: center;

              .mecharts-box {
                margin-right: 10px;
              }

              .last-item {
                text-align: right;
                color: $text_color;
                font-weight: 700;
                font-size: 12px;
              }
            }
          }
        }
      }

      .more-box {
        height: 40px;

        .icon-group {
          display: flex;
          justify-content: center;
          align-items: center;
          height: 40px;
          line-height: 40px;
          font-size: 14px;

          .icon.arrow {
            margin-left: 10px;
            width: 7px;
            height: 9px;
          }
        }
      }

    }

  }


}
</style>